package com.szbmyoubao.app.helper

import android.content.ComponentName
import android.content.pm.PackageManager
import com.ellecity06.common.utils.appContext

/**
 * @author ellecity06
 * @time 2025/1/7 16:14
 * @des
 */
class LauncherIconHelper {

    companion object {
        /**
         * 正常启动页
         */
        private const val LAUNCHER_ICON_NORMAL: String = "Default"

        /**
         * 春节启动页
         */
        private const val LAUNCHER_ICON_NEW_YEAR: String = "NewYear"

        /**
         * 元宵节启动页
         */
        private const val LAUNCHER_ICON_LANTERN_FESTIVAL: String = "LanternFestival"

        fun defaultLauncherIcon() {
            changeLauncherIcon(LAUNCHER_ICON_NORMAL)
        }

        /**
         * 春节启动页
         */
        fun newYearLauncherIcon() {
            changeLauncherIcon(LAUNCHER_ICON_NEW_YEAR)
        }

        /**
         * 元宵节启动页
         */
        fun festivalLauncherIcon() {
            changeLauncherIcon(LAUNCHER_ICON_LANTERN_FESTIVAL)
        }

        private fun changeLauncherIcon(enabledIcon: String) {
            val pm = appContext.packageManager
            val listOfLauncher = listOf(LAUNCHER_ICON_NORMAL, LAUNCHER_ICON_NEW_YEAR, LAUNCHER_ICON_LANTERN_FESTIVAL)
            //enable launcher className
            pm.setComponentEnabledSetting(
                getComponentClassName(enabledIcon),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP
            )

            // disable rest of list
            val disabledIconList = listOfLauncher.filterNot { it == enabledIcon }
            for (disabledIcon in disabledIconList) {
                pm.setComponentEnabledSetting(
                    getComponentClassName(disabledIcon),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP
                )
            }

        }

        private fun getComponentClassName(name: String): ComponentName {
            return ComponentName(appContext.packageName, "com.ellecity06.cwhiskymall.ui.splash.$name")
        }
    }
}